package com.dhsc.util;

import java.util.ArrayList;
import java.util.List;

import com.dhsc.domain.yypt.yhzx.MenuVo;

/**
 * @ClassName:  MenuTreeUtil
 * @Description: 菜单权限工具类
 * @date:   2023年6月23日 下午10:40:24       
 * @author: clong
 * @Copyright:  东海运营
 */
public class MenuTreeUtil {
	
	private List<MenuVo> menuList = new ArrayList<>();
	
	public MenuTreeUtil(List<MenuVo> menuList) {
		this.menuList = menuList;
	}

	//建立树形结构
	public List<MenuVo> builTree(){
		List<MenuVo> treeMenus =new  ArrayList<>();
		for(MenuVo menuNode : getRootNode()) {
			buildChilTree(menuNode, menuNode.getText(), menuNode.getId());
			menuNode.setHref(CommonUtil.safeString(menuNode.getHref()));
			treeMenus.add(menuNode);
		}
		return treeMenus;
	}

	//递归，建立子树形结构
	private MenuVo buildChilTree(MenuVo pNode, String topcdmc, String topcdid){
		List<MenuVo> chilMenus =new  ArrayList<>();
		for(MenuVo menuNode : menuList) {
			if(menuNode.getPid().equals(pNode.getId())) {
				chilMenus.add(buildChilTree(menuNode, topcdmc, topcdid));
			}
		}
		pNode.setChild(chilMenus);
		pNode.setChildren(chilMenus);
		pNode.setTopcdmc(topcdmc);
		pNode.setTopcdid(topcdid);
		return pNode;
	}

	//获取根节点
	private List<MenuVo> getRootNode() {
		List<MenuVo> rootMenuLists =new  ArrayList<>();
	    for(MenuVo menuNode : menuList) {
	        if(menuNode.getPid().equals("0")) {
	            rootMenuLists.add(menuNode);
	        }
	    }
	    return rootMenuLists;
	}
}
